
const app = getApp();

Page({
  data: {
    listData: {
      onItemTap: 'handleListItemTap',
      data: [],
      longTap :'handleListLongTap',
    },
    rid:"",
  },

  onLoad: function (options) {
    var rid = options.rid;
    this.setData({
        rid:rid,
      })
  },

  handleListItemTap(e) {
    var url = app.globalData.url;
    var index = e.currentTarget.dataset.index;
    var id = this.data.listData.data[index].id;
    var goal='';
    var result='';
    var plan='';
    var rid='';
    var followData='';
    var followMan = "";
    dd.httpRequest({
          url: url+'/api/hr/weekly/list',
          method: 'POST',
          data: {
            id:id,
          },
          dataType: 'json',
          success: function(res) {
              res.data.data.forEach(function(index){
                  goal=index.goal;
                  result=index.result;
                  plan=index.plan;
                  rid=index.rid;
                  followData=index.followData;
                  followMan=index.followMan;
              })
              dd.navigateTo({
                url: '../../weekly/weekly?id='+id+'&goal='+goal+'&result='+result+'&plan='+plan+'&rid='+rid+'&followData='+followData+'&followMan='+followMan
              })
          },
          fail: function(res) {
            dd.alert({content: 'fail'});
          },
          complete: function(res) {
            // dd.alert({content: 'complete'});
          }
      });
  },
  selTime(e){
    var that =this;
    var name =e.detail.value;
    var isAdmin=app.globalData.userinfo.isAdmin;  //管理员默认全部可查
    var isBoss= app.globalData.userinfo.isBoss;  //老板全部可查
    var isLeaderInDepts=app.globalData.userinfo.isLeaderInDepts; //是否为部门主管   String类型！
    var isDept =isLeaderInDepts.split(":") //分割字符后 前后有{}
    if(isBoss||isAdmin){
      that.selweekList(1,name);
    }else if(isDept[1]=='true}'){
      that.selweekList(2,name);
    }else{
      that.selweekList(3,name);
    }

  },
  selweekList(type,name){
    var that =this;
    var Uid = app.globalData.userinfo.userid;
    var url = app.globalData.url;
    var department=app.globalData.userinfo.department;    //部门id

    var summList =[];
      dd.httpRequest({ 
            url:url+'/api/hr/weekly/history',
            method:'post',
            datatype:'json',
            data:{
                departmentid:department,
                Uid:Uid,
                type:type,
                name:name,
                rid:that.data.rid,
            },
            success:function(res){
              if(res.data.data==""){
                that.setData({
                    'listData.data':[],
                })
              }else{
                res.data.data.forEach(function (item,index){
                  summList.push({
                      'title': res.data.data[index].followData,
                      'arrow': 'horizontal',
                      'extra': res.data.data[index].followMan,
                      'id': res.data.data[index].id
                  })
                  that.setData({
                    'listData.data':summList,
                  })
               });
              }
              
            } 
     });
  },
 onShow(options) {
    var that =this;
    var isAdmin=app.globalData.userinfo.isAdmin;  //管理员默认全部可查
    var isBoss= app.globalData.userinfo.isBoss;  //老板全部可查
    var isLeaderInDepts=app.globalData.userinfo.isLeaderInDepts; //是否为部门主管   String类型！
    var isDept =isLeaderInDepts.split(":") //分割字符后 前后有{}
    var name='';
    if(isBoss||isAdmin){
      that.selweekList(1,name);
    }else if(isDept[1]=='true}'){
      that.selweekList(2,name);
    }else{
      that.selweekList(3,name);
    }


  },

  handleListLongTap(e){
    var that=this;
    var url = app.globalData.url;
    var index = e.currentTarget.dataset.index;
    var id = this.data.listData.data[index].id;
      dd.confirm({
        title: '温馨提示',
        content: '您确认要删除该商机周报',
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        success: function(confirm){
            if(confirm.confirm){
                dd.httpRequest({ 
                  url:url+'/api/hr/weekly/remove',
                  method:'post',
                  datatype:'json',
                  data:{
                    ids:id,
                  },
                  success:function(res){
                    that.onShow();
                  } 
                });
            }
        }
      });
      
  },
});
